import axios, {  AxiosResponse, InternalAxiosRequestConfig } from "axios";
import { getToken } from "./token";

const request = axios.create({
  baseURL: "http://127.0.0.1:8888/api/private/v1/",
  timeout: 5000,
});

request.interceptors.request.use(
  (config:InternalAxiosRequestConfig) => {
    if (getToken()) {
      config.headers.Authorization = getToken();
    }
    return config;
  },
  (error:string) => {
    return Promise.reject(error);
  }
);

 request.interceptors.response.use(
  (response:AxiosResponse) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default request;